home *** CD-ROM | disk | FTP | other *** search
- package java.net;
-
- import java.io.FileDescriptor;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.security.AccessController;
- import sun.net.ConnectionResetException;
- import sun.security.action.LoadLibraryAction;
-
- class PlainSocketImpl extends SocketImpl {
- int timeout;
- private int trafficClass;
- private boolean shut_rd = false;
- private boolean shut_wr = false;
- private SocketInputStream socketInputStream = null;
- private int fdUseCount = 0;
- private Object fdLock = new Object();
- private boolean closePending = false;
- private int CONNECTION_NOT_RESET = 0;
- private int CONNECTION_RESET_PENDING = 1;
- private int CONNECTION_RESET = 2;
- private int resetState;
- private Object resetLock = new Object();
- private FileDescriptor fd1;
- private InetAddress anyLocalBoundAddr = null;
- private int lastfd = -1;
- public static final int SHUT_RD = 0;
- public static final int SHUT_WR = 1;
-
- PlainSocketImpl() {
- }
-
- PlainSocketImpl(FileDescriptor var1) {
- this.fd = var1;
- }
-
- protected synchronized void create(boolean var1) throws IOException {
- this.fd = new FileDescriptor();
- this.fd1 = new FileDescriptor();
- this.socketCreate(var1);
- if (this.socket != null) {
- this.socket.setCreated();
- }
-
- if (this.serverSocket != null) {
- this.serverSocket.setCreated();
- }
-
- }
-
- protected void connect(String var1, int var2) throws UnknownHostException, IOException {
- Object var3 = null;
-
- try {
- InetAddress var4 = InetAddress.getByName(var1);
-
- try {
- this.connectToAddress(var4, var2, this.timeout);
- return;
- } catch (IOException var6) {
- var3 = var6;
- }
- } catch (UnknownHostException var7) {
- var3 = var7;
- }
-
- this.close();
- throw var3;
- }
-
- protected void connect(InetAddress var1, int var2) throws IOException {
- this.port = var2;
- this.address = var1;
-
- try {
- this.connectToAddress(var1, var2, this.timeout);
- } catch (IOException var4) {
- this.close();
- throw var4;
- }
- }
-
- protected void connect(SocketAddress var1, int var2) throws IOException {
- if (var1 != null && var1 instanceof InetSocketAddress) {
- InetSocketAddress var3 = (InetSocketAddress)var1;
- if (var3.isUnresolved()) {
- throw new UnknownHostException(var3.getHostName());
- } else {
- this.port = var3.getPort();
- this.address = var3.getAddress();
-
- try {
- this.connectToAddress(this.address, this.port, var2);
- } catch (IOException var5) {
- this.close();
- throw var5;
- }
- }
- } else {
- throw new IllegalArgumentException("unsupported address type");
- }
- }
-
- private void connectToAddress(InetAddress var1, int var2, int var3) throws IOException {
- if (var1.isAnyLocalAddress()) {
- this.doConnect(InetAddress.getLocalHost(), var2, var3);
- } else {
- this.doConnect(var1, var2, var3);
- }
-
- }
-
- public void setOption(int var1, Object var2) throws SocketException {
- if (this.isClosedOrPending()) {
- throw new SocketException("Socket Closed");
- } else {
- boolean var3 = true;
- switch (var1) {
- case 1:
- if (var2 == null || !(var2 instanceof Boolean)) {
- throw new SocketException("bad parameter for TCP_NODELAY");
- }
-
- var3 = (Boolean)var2;
- break;
- case 3:
- if (var2 == null || !(var2 instanceof Integer)) {
- throw new SocketException("bad argument for IP_TOS");
- }
-
- this.trafficClass = (Integer)var2;
- break;
- case 4:
- if (var2 == null || !(var2 instanceof Boolean)) {
- throw new SocketException("bad parameter for SO_REUSEADDR");
- }
-
- var3 = (Boolean)var2;
- break;
- case 8:
- if (var2 == null || !(var2 instanceof Boolean)) {
- throw new SocketException("bad parameter for SO_KEEPALIVE");
- }
-
- var3 = (Boolean)var2;
- break;
- case 15:
- throw new SocketException("Cannot re-bind socket");
- case 128:
- if (var2 == null || !(var2 instanceof Integer) && !(var2 instanceof Boolean)) {
- throw new SocketException("Bad parameter for option");
- }
-
- if (var2 instanceof Boolean) {
- var3 = false;
- }
- break;
- case 4097:
- case 4098:
- if (var2 == null || !(var2 instanceof Integer) || (Integer)var2 <= 0) {
- throw new SocketException("bad parameter for SO_SNDBUF or SO_RCVBUF");
- }
- break;
- case 4099:
- if (var2 == null || !(var2 instanceof Boolean)) {
- throw new SocketException("bad parameter for SO_OOBINLINE");
- }
-
- var3 = (Boolean)var2;
- break;
- case 4102:
- if (var2 == null || !(var2 instanceof Integer)) {
- throw new SocketException("Bad parameter for SO_TIMEOUT");
- }
-
- int var4 = (Integer)var2;
- if (var4 < 0) {
- throw new IllegalArgumentException("timeout < 0");
- }
-
- this.timeout = var4;
- break;
- default:
- throw new SocketException("unrecognized TCP option: " + var1);
- }
-
- this.socketSetOption(var1, var3, var2);
- }
- }
-
- public Object getOption(int var1) throws SocketException {
- if (this.isClosedOrPending()) {
- throw new SocketException("Socket Closed");
- } else if (var1 == 4102) {
- return new Integer(this.timeout);
- } else {
- int var2 = 0;
- switch (var1) {
- case 1:
- var2 = this.socketGetOption(var1, (Object)null);
- return var2 != -1;
- case 3:
- var2 = this.socketGetOption(var1, (Object)null);
- if (var2 == -1) {
- return new Integer(this.trafficClass);
- }
-
- return new Integer(var2);
- case 4:
- var2 = this.socketGetOption(var1, (Object)null);
- return var2 != -1;
- case 8:
- var2 = this.socketGetOption(var1, (Object)null);
- return var2 != -1;
- case 15:
- if (this.fd != null && this.fd1 != null) {
- return this.anyLocalBoundAddr;
- }
-
- InetAddressContainer var3 = new InetAddressContainer();
- this.socketGetOption(var1, var3);
- return var3.addr;
- case 128:
- var2 = this.socketGetOption(var1, (Object)null);
- return var2 == -1 ? Boolean.FALSE : new Integer(var2);
- case 4097:
- case 4098:
- var2 = this.socketGetOption(var1, (Object)null);
- return new Integer(var2);
- case 4099:
- var2 = this.socketGetOption(var1, (Object)null);
- return var2 != -1;
- default:
- return null;
- }
- }
- }
-
- private synchronized void doConnect(InetAddress var1, int var2, int var3) throws IOException {
- try {
- FileDescriptor var4 = this.acquireFD();
-
- try {
- this.socketConnect(var1, var2, var3);
- if (this.socket != null) {
- this.socket.setBound();
- this.socket.setConnected();
- }
- } finally {
- this.releaseFD();
- }
-
- } catch (IOException var9) {
- this.close();
- throw var9;
- }
- }
-
- protected synchronized void bind(InetAddress var1, int var2) throws IOException {
- this.socketBind(var1, var2);
- if (this.socket != null) {
- this.socket.setBound();
- }
-
- if (this.serverSocket != null) {
- this.serverSocket.setBound();
- }
-
- if (var1.isAnyLocalAddress()) {
- this.anyLocalBoundAddr = var1;
- }
-
- }
-
- protected synchronized void listen(int var1) throws IOException {
- this.socketListen(var1);
- }
-
- protected synchronized void accept(SocketImpl var1) throws IOException {
- FileDescriptor var2 = this.acquireFD();
-
- try {
- this.socketAccept(var1);
- } finally {
- this.releaseFD();
- }
-
- }
-
- protected synchronized InputStream getInputStream() throws IOException {
- if (this.isClosedOrPending()) {
- throw new IOException("Socket Closed");
- } else if (this.shut_rd) {
- throw new IOException("Socket input is shutdown");
- } else {
- if (this.socketInputStream == null) {
- this.socketInputStream = new SocketInputStream(this);
- }
-
- return this.socketInputStream;
- }
- }
-
- void setInputStream(SocketInputStream var1) {
- this.socketInputStream = var1;
- }
-
- protected synchronized OutputStream getOutputStream() throws IOException {
- if (this.isClosedOrPending()) {
- throw new IOException("Socket Closed");
- } else if (this.shut_wr) {
- throw new IOException("Socket output is shutdown");
- } else {
- return new SocketOutputStream(this);
- }
- }
-
- protected synchronized int available() throws IOException {
- if (this.isClosedOrPending()) {
- throw new IOException("Stream closed.");
- } else if (this.isConnectionReset()) {
- return 0;
- } else {
- int var1 = 0;
-
- try {
- var1 = this.socketAvailable();
- if (var1 == 0 && this.isConnectionResetPending()) {
- this.setConnectionReset();
- }
- } catch (ConnectionResetException var5) {
- this.setConnectionResetPending();
-
- try {
- var1 = this.socketAvailable();
- if (var1 == 0) {
- this.setConnectionReset();
- }
- } catch (ConnectionResetException var4) {
- }
- }
-
- return var1;
- }
- }
-
- protected void close() throws IOException {
- synchronized(this.fdLock) {
- if (this.fd != null || this.fd1 != null) {
- if (this.fdUseCount == 0) {
- if (this.closePending) {
- return;
- }
-
- this.closePending = true;
-
- try {
- this.socketPreClose();
- } finally {
- this.socketClose();
- }
-
- this.fd = null;
- this.fd1 = null;
- return;
- }
-
- if (!this.closePending) {
- this.closePending = true;
- --this.fdUseCount;
- this.socketPreClose();
- }
- }
-
- }
- }
-
- protected void shutdownInput() throws IOException {
- if (this.fd != null) {
- this.socketShutdown(0);
- if (this.socketInputStream != null) {
- this.socketInputStream.setEOF(true);
- }
-
- this.shut_rd = true;
- }
-
- }
-
- protected void shutdownOutput() throws IOException {
- if (this.fd != null) {
- this.socketShutdown(1);
- this.shut_wr = true;
- }
-
- }
-
- protected boolean supportsUrgentData() {
- return true;
- }
-
- protected void sendUrgentData(int var1) throws IOException {
- if (this.fd == null) {
- throw new IOException("Socket Closed");
- } else {
- this.socketSendUrgentData(var1);
- }
- }
-
- protected void finalize() throws IOException {
- this.close();
- }
-
- public final FileDescriptor acquireFD() {
- synchronized(this.fdLock) {
- ++this.fdUseCount;
- return this.fd;
- }
- }
-
- public final void releaseFD() {
- synchronized(this.fdLock) {
- --this.fdUseCount;
- if (this.fdUseCount == -1 && this.fd != null) {
- try {
- this.socketClose();
- } catch (IOException var8) {
- } finally {
- this.fd = null;
- }
- }
-
- }
- }
-
- public boolean isConnectionReset() {
- synchronized(this.resetLock) {
- return this.resetState == this.CONNECTION_RESET;
- }
- }
-
- public boolean isConnectionResetPending() {
- synchronized(this.resetLock) {
- return this.resetState == this.CONNECTION_RESET_PENDING;
- }
- }
-
- public void setConnectionReset() {
- synchronized(this.resetLock) {
- this.resetState = this.CONNECTION_RESET;
- }
- }
-
- public void setConnectionResetPending() {
- synchronized(this.resetLock) {
- if (this.resetState == this.CONNECTION_NOT_RESET) {
- this.resetState = this.CONNECTION_RESET_PENDING;
- }
-
- }
- }
-
- public boolean isClosedOrPending() {
- synchronized(this.fdLock) {
- return this.closePending || this.fd == null && this.fd1 == null;
- }
- }
-
- public int getTimeout() {
- return this.timeout;
- }
-
- private void socketPreClose() throws IOException {
- this.socketClose0(true);
- }
-
- private void socketClose() throws IOException {
- this.socketClose0(false);
- }
-
- private native void socketCreate(boolean var1) throws IOException;
-
- private native void socketConnect(InetAddress var1, int var2, int var3) throws IOException;
-
- private native void socketBind(InetAddress var1, int var2) throws IOException;
-
- private native void socketListen(int var1) throws IOException;
-
- private native void socketAccept(SocketImpl var1) throws IOException;
-
- private native int socketAvailable() throws IOException;
-
- private native void socketClose0(boolean var1) throws IOException;
-
- private native void socketShutdown(int var1) throws IOException;
-
- private static native void initProto();
-
- private native void socketSetOption(int var1, boolean var2, Object var3) throws SocketException;
-
- private native int socketGetOption(int var1, Object var2) throws SocketException;
-
- private native int socketGetOption1(int var1, Object var2, FileDescriptor var3) throws SocketException;
-
- private native void socketSendUrgentData(int var1) throws IOException;
-
- static {
- AccessController.doPrivileged(new LoadLibraryAction("net"));
- initProto();
- }
- }
-